package com.example.fufudamowang.greendao;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.fufudamowang.DaoMaster;
import com.example.fufudamowang.DaoSession;
import com.example.fufudamowang.Note;
import com.example.fufudamowang.NoteDao;

import java.text.DateFormat;
import java.util.Date;

public class insert1 extends AppCompatActivity {
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private NoteDao noteDao;



    private EditText title;
    private EditText body;
    private Button button;
    private Note note;
    private long mid;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);


        InitDAO();

        title=(EditText)findViewById(R.id.title);
        body=(EditText)findViewById(R.id.body);
        button=(Button)findViewById(R.id.BB);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String note_title=title.getText().toString();
                String note_body=body.getText().toString();
                Note note=new Note();
                note.setTitle(note_title);
                note.setBody(note_body);
                final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
                        DateFormat.MEDIUM);
                String date = df.format(new Date());
                note.setDate(date);
                noteDao.insert(note);
                Toast.makeText(insert1.this,"创建成功",Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }
    private void InitDAO() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
                null);
        db = helper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
        noteDao = daoSession.getNoteDao();
    }

}
